package com.example.scorer

import android.util.Log
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.SavedStateHandle
import kotlin.math.log

class MyViewModel(handle: SavedStateHandle) : ViewModel() {
    //声明变量：两队的分数，使用MutableLiveData
    private  var teamAScore:MutableLiveData<Int>?=null
    private  var teamBScore:MutableLiveData<Int>?=null
    //撤回信息数组
    private var list:MutableLiveData<ArrayList<Undo>>? = null
    private var mhandle=handle


    /**
     * 获取A队分数
     */
    fun getTeanAScore(): MutableLiveData<Int>{
        if (teamAScore==null){
            if (!(mhandle!!.contains(MainActivity.SCOREA))){
                mhandle!!.set(MainActivity.SCOREA,0)
            }
            teamAScore= mhandle!!.getLiveData(MainActivity.SCOREA)
            return teamAScore as MutableLiveData<Int>
        }else{
            return teamAScore as MutableLiveData<Int>
        }
//

    }

    /**
     * 获取撤销数组
     */
    fun getList(): MutableLiveData<ArrayList<Undo>>? {
        if (!(mhandle!!.contains(MainActivity.LIST))){
            var mlist= arrayListOf<Undo>()
            mhandle.set(MainActivity.LIST, mlist)
        }
        list=mhandle.getLiveData(MainActivity.LIST)
        return list
    }
    /**
     * 获取B队分数
     */
    fun getTeanBScore():MutableLiveData<Int>{
        if (teamBScore==null){
            if (!(mhandle!!.contains(MainActivity.SCOREB))){
                mhandle!!.set(MainActivity.SCOREB,0)
            }
            teamBScore= mhandle!!.getLiveData(MainActivity.SCOREB)
            return teamBScore as MutableLiveData<Int>
        }else{
            return teamBScore as MutableLiveData<Int>
        }
    }

    /**
     * A队加分
     */
    fun addScore2A(n:Int){
        if (list==null){
            getList()
        }
        list!!.value!!.add(Undo("A", teamAScore?.value!!))
        teamAScore?.value = teamAScore?.value?.plus(n)


    }

    /**
     * B队伍加分
     */
    fun addScore2B(n:Int){
        if (list==null){
            getList()
        }
        list!!.value!!.add(Undo("B", teamBScore?.value!!))
        teamBScore?.value = teamBScore?.value?.plus(n)


    }

    /**
     * 重置
     */
    fun reset(){
        list!!.value!!.add(Undo("A", teamAScore?.value!!))
        list!!.value!!.add(Undo("B", teamBScore?.value!!))
        teamAScore.also {
            it!!.value=0
        }

        teamBScore.also {
            it!!.value=0
        }

    }

    /**
     * 撤回
     */
    fun undo(){
      if (list!!.value!!.size>0){
          var undo=list!!.value!![list!!.value!!.size-1]

          when(undo.team){
              "A"->{
                  teamAScore?.value = undo.n
                  Log.i("score","A 加分：${teamAScore?.value!!}")
              }
              "B"->{
                  teamBScore?.value = undo.n
                  Log.i("score","B 加分：${teamBScore?.value!!}")
              }
          }
          list!!.value!!.removeAt(list!!.value!!.size-1)
      }
    }
}